#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int n;
vector<int> v1, v2;
string s1, s2;

int main()
{
    cin >> n;
    cin >> s1 >> s2;
    for (int i = 0; i < n; ++i)
    {
        string tmp;
        cin >> tmp;
        if (tmp == s1) v1.emplace_back(i);
        else if (tmp == s2) v2.emplace_back(i);
    }
    if (v1.empty() || v2.empty()) cout << -1 << endl;
    else {
        int ans = INT_MAX;
        for (auto p1 : v1) {
            int l = 0, r = v2.size() - 1;
            while (l < r)
            {
                int mid = (l + r + 1) >> 1;
                if (v2[mid] <= p1)
                    l = mid;
                else
                    r = mid - 1;
            }
            ans = min(ans, abs(p1 - v2[l]));
            if (l != v2.size() - 1)
                ans = min(ans, abs(p1 - v2[l + 1]));
        }
        cout << ans << endl;
    }
    return 0;
}